package com.ljd.sps;

import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class CropCanvas extends View {
    private static final String TAG = "ljd";

    private int mDefaultChooseAreaWidth = 200;
    private int mDefaultChooseAreaHeight = 200;

    private final static int PRESS_LB = 0;//表示左下角矩形框
    private final static int PRESS_LT = 1;//表示左上角矩形框
    private final static int PRESS_RB = 2;//表示右下角矩形框
    private final static int PRESS_RT = 3;//表示右上角矩形框
    private final static int PRESS_L = 4; //表示左边矩形框
    private final static int PRESS_T = 5; //表示顶边矩形框
    private final static int PRESS_R = 6; //表示右边边矩形框
    private final static int PRESS_B = 7; //表示底部边矩形框

    private final static int CORNER_SIZE = 11;
    private final static int MARGIN_WIDTH = 22;
    private final static int MARGIN_HEIGHT = 63;

    private final static int COLOR_NORMAL = Color.GREEN;//正常状态下选框的颜色
    private final static int COLOR_PRESSED = Color.RED;//点击状态下选框的颜色
    private final static int MIN_SIZE = MARGIN_HEIGHT;

    private RectF dst = null; //图片显示区域，也就是drawBitmap函数中的目标dst
    private RectF ChooseArea = null; //选择区域				
    private Paint mPaint = null; //画笔

    private int mx = 0; //存储触笔移动时，之前所在的触笔的x坐标
    private int my = 0; //存储触笔移动时，之前所在的触笔的y坐标
    private boolean touchFlag = false; //触笔是否在屏幕之上
    private boolean cutFlag = false; //是否点击了menu上的裁剪按钮
    private int recFlag = -1; //用来存储触笔点击了哪个小矩形框（改变选择区域大小的小矩形框）
    private boolean firstFlag = false;

    private RectF recLT = null; //左上角的小矩形框
    private RectF recRT = null; //右上角的小矩形框
    private RectF recLB = null; //左下角的小矩形框
    private RectF recRB = null; //右下角的小矩形框
    private RectF recL = null; //左边小矩形框
    private RectF recT = null; //上边小矩形框
    private RectF recR = null; //右边小矩形框
    private RectF recB = null; //下边小矩形框
    private static final int LEFT_AREA_ALPHA = 50 * 255 / 100;
    private RectF leftRectL = null;
    private RectF leftRectR = null;
    private RectF leftRectT = null;
    private RectF leftRectB = null;
    private Paint leftAreaPaint = null;

    private int mType;

    public CropCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public CropCanvas(Context context) {
        super(context);
        this.init();
    }

    public void init() {
        cutFlag = true;
        recLT = new RectF();
        recLB = new RectF();
        recRT = new RectF();
        recRB = new RectF();
        recL = new RectF();
        recT = new RectF();
        recR = new RectF();
        recB = new RectF();
        dst = new RectF();
        mPaint = new Paint();
        mPaint.setColor(COLOR_NORMAL);
        mPaint.setStyle(Paint.Style.STROKE); //将画笔的风格改为空心
        mPaint.setStrokeWidth(5);
        firstFlag = true;

        //选择框之外的灰色区域，分成四个矩形框

        leftRectB = new RectF();
        leftRectL = new RectF();
        leftRectR = new RectF();
        leftRectT = new RectF();

        leftAreaPaint = new Paint();
        leftAreaPaint.setStyle(Paint.Style.FILL);
        leftAreaPaint.setAlpha(CropCanvas.LEFT_AREA_ALPHA);

        if (SmallphoneScannerActivity.LOG) {
            Log.d(TAG, "crop canvas init finished");
        }
    }

    public void setType(int type) {
        mType = type;
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
        RectF lastChooseArea = Util.getLastChooseArea(sp, type);

        if (lastChooseArea != null) {
            ChooseArea = lastChooseArea;
        } else {
            switch (type) {
            case SmallphoneScannerActivity._NEGATIVES:
            case SmallphoneScannerActivity._SLIDER:
                if (Util.isS3()) {
                    ChooseArea = new RectF(253, 340.5f, 492, 768.5f);
                } else if (Util.isS4()) {
                    ChooseArea = new RectF(228, 464, 794, 1212);
                } else {
                    ChooseArea = new RectF(158, 180, 333, 466);
                }
                break;
            case SmallphoneScannerActivity._3X5:
                if (Util.isS3()) {
                    ChooseArea = new RectF(80, 124.5f, 579, 975.5f);
                } else if (Util.isS4()) {
                    ChooseArea = new RectF(0, 138, 994, 1580);
                } else {
                    ChooseArea = new RectF(38, 50, 395, 605);
                }
                break;
            case SmallphoneScannerActivity._4X6:
                if (Util.isS3()) {
                    ChooseArea = new RectF(78, 33.5f, 653, 1063.5f);
                } else if (Util.isS4()) {
                    ChooseArea = new RectF(0, 6, 1072, 1680);
                } else {
                    ChooseArea = new RectF(37, 0, 449, 663);
                }
                break;
            }
        }
        setPressRecLoc();
    }

    //	public void setBitmap(Bitmap bitmap){
    //		BitmapDrawable bd = new BitmapDrawable(bitmap);
    //		src = new RectF(0,0,bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
    //		this.bitMap = bitmap.copy(Config.ARGB_8888, true);
    //		
    //		this.setImageBitmap(bitMap);
    //		leftRectB = new RectF();
    //		leftRectL = new RectF();
    //		leftRectR = new RectF();
    //		leftRectT = new RectF();
    //	}

    //	public void imageScale(){
    //		matrix = this.getImageMatrix();
    //		matrix.mapRect(dst, src);
    //		int padding = this.getPaddingBottom();
    //		dst.set(dst.left+padding,dst.top+padding,dst.right+padding,dst.bottom+padding);
    //		ChooseArea = new RectF(dst);
    //		this.setPressRecLoc();
    //	}

    //裁剪出选择区域里的图片
    //之前要做比例转换，因为，你选择的区域是针对比例转换后的图片
    //所以要使用ChooseArea的数值做比例转换，然后，使用这些数值重新设置rec的大小
    public Bitmap getSubsetBitmap(byte[] srcBitMap) {
        BitmapRegionDecoder regionDecoder;
        try {
            regionDecoder = BitmapRegionDecoder.newInstance(srcBitMap, 0, srcBitMap.length, true);
            if (SmallphoneScannerActivity.LOG) {
                Log.i(TAG, "srcBitMap:" + regionDecoder.getHeight() + ":" + regionDecoder.getWidth());
                Log.i(TAG, "dst:" + (dst.right - dst.left) + ":" + (dst.bottom - dst.top));
            }
            float padding = 0;
            if (Util.isS4()) {
                padding = 380;
            }
            float ratioWidth = (regionDecoder.getHeight() - padding * 2) / (float) (dst.right - dst.left);
            float ratioHeight = (regionDecoder.getWidth()) / (float) (dst.bottom - dst.top);
            int y = (int) ((dst.right - ChooseArea.right) * ratioWidth + padding);
            int height = (int) ((ChooseArea.right - ChooseArea.left) * ratioWidth);
            int x = (int) ((ChooseArea.top - dst.top) * ratioHeight);
            int width = (int) ((ChooseArea.bottom - ChooseArea.top) * ratioHeight);
            if (SmallphoneScannerActivity.LOG) {
                Log.i(TAG, "ratio" + ratioWidth + ":" + ratioHeight);
                Log.i(TAG, "size:" + y + "|" + height + "|" + x + "|" + width);
            }
            firstFlag = true;
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
            Util.setLastChooseArea(sp, mType, ChooseArea);
            set_LeftArea_Alpha();
            Bitmap result = regionDecoder.decodeRegion(new Rect(x, y, x + width, y + height), null);
            regionDecoder.recycle();
            regionDecoder = null;

            System.gc();
            return result;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    private float[] getPaddingSize(float srcWidth, float srcHeight, float destWidth, float destHeight) {
        float[] result = new float[] { 0, 0 };
        if (srcWidth / srcHeight > destWidth / destHeight) {
            result[0] = (srcWidth - (destWidth * srcHeight / destHeight));
        } else {
            result[1] = (srcHeight - (destHeight * srcWidth / destWidth));
        }
        return result;
    }

    //获得ChooseArea对象
    public RectF getChooseArea() {
        return ChooseArea;
    }

    //移动选择区域，选择区域是不能从图片区域里移出去的
    public void moveChooseArea(int move_x, int move_y) {
        if (ChooseArea.left + move_x >= dst.left && ChooseArea.right + move_x <= dst.right
                && ChooseArea.top + move_y >= dst.top && ChooseArea.bottom + move_y <= dst.bottom) {
            ChooseArea.set(ChooseArea.left + move_x, ChooseArea.top + move_y, ChooseArea.right + move_x,
                    ChooseArea.bottom + move_y);
        } else {
            if (ChooseArea.left + move_x < dst.left) {
                ChooseArea.set(dst.left, ChooseArea.top, ChooseArea.right + dst.left - ChooseArea.left,
                        ChooseArea.bottom);
            }
            if (ChooseArea.right + move_x > dst.right) {
                ChooseArea.set(ChooseArea.left + dst.right - ChooseArea.right, ChooseArea.top, dst.right,
                        ChooseArea.bottom);
            }

            if (ChooseArea.top + move_y < dst.top) {
                ChooseArea
                        .set(ChooseArea.left, dst.top, ChooseArea.right, ChooseArea.bottom + dst.top - ChooseArea.top);
            }

            if (ChooseArea.bottom + move_y > dst.bottom) {
                ChooseArea.set(ChooseArea.left, ChooseArea.top + dst.bottom - ChooseArea.bottom, ChooseArea.right,
                        dst.bottom);
            }
        }
        setPressRecLoc();
        mPaint.setColor(COLOR_PRESSED);
        this.invalidate();
    }

    public boolean onTouchEvent(MotionEvent event) {
        mPaint.setColor(COLOR_NORMAL);

        //点击了裁剪按钮之后才会执行以下事件
        if (event.getAction() == MotionEvent.ACTION_DOWN && cutFlag) {
            //System.out.println(event.getX() + "," + event.getY());

            mx = (int) event.getX();
            my = (int) event.getY();

            //判断触笔是否在改变区域大小的小矩形框之内
            if (this.findPresseddst((int) event.getX(), (int) event.getY())) {
                touchFlag = true;
                mPaint.setColor(COLOR_NORMAL);
                return true;
            }

            //判断触笔是否在裁剪区域内，也就是ChooseArea内
            //如果是，整个区域随着鼠标移动而移动
            if (this.judgeLocation(mx, my)) {
                touchFlag = true;
                mPaint.setColor(COLOR_PRESSED);
                this.invalidate();
                return true;
            }
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE && touchFlag) {
            //判断是否点击了哪个个小矩形框
            if (this.isOutOfArea((int) event.getX(), (int) event.getY())) {
                return true;
            }

            //如果选择区域大小跟图像大小一样时，就不能移动
            if (ChooseArea.left == dst.left && ChooseArea.top == dst.top && ChooseArea.right == dst.right
                    && ChooseArea.bottom == dst.bottom) {
            } else {
                this.moveChooseArea((int) event.getX() - mx, (int) event.getY() - my);
                mx = (int) event.getX();
                my = (int) event.getY();
            }
        }

        //触笔移出屏幕时，将一些变量设回初值
        if (event.getAction() == MotionEvent.ACTION_UP) {
            recFlag = -1;
            this.invalidate();
            touchFlag = false;
        }

        return super.onTouchEvent(event);
    }

    //判断是否要超出图片区域，这个函数会调用下面四个press打头的函数
    //这个函数也会重绘整个画布，也就是选择区域会随着鼠标的移动改变
    private boolean isOutOfArea(int x, int y) {
        switch (recFlag) {
        case CropCanvas.PRESS_LB:
            this.pressLB(x - mx, y - my);
            break;
        case CropCanvas.PRESS_LT:
            this.pressLT(x - mx, y - my);
            break;
        case CropCanvas.PRESS_RB:
            this.pressRB(x - mx, y - my);
            break;
        case CropCanvas.PRESS_RT:
            this.pressRT(x - mx, y - my);
            break;
        case CropCanvas.PRESS_L:
            pressL(x - mx);
            break;
        case CropCanvas.PRESS_T:
            pressT(y - my);
            break;
        case CropCanvas.PRESS_R:
            pressR(x - mx);
            break;
        case CropCanvas.PRESS_B:
            pressB(y - my);
            break;
        default:
            return false;
        }
        mx = x;
        my = y;
        this.invalidate();
        return true;
    }

    //找到点击了哪个矩形框（改变选择区域大小的小矩形框）
    //这个是在MotionEvent.ACTION_DOWN这个动作时执行的
    //是为了在MotionEvent.ACTION_MOVE的时候，知道应该移动哪个小矩形框
    public boolean findPresseddst(int x, int y) {
        boolean returnFlag = false;
        if (this.isInRect(x, y, recLB)) {
            recFlag = CropCanvas.PRESS_LB;
            returnFlag = true;
        } else if (this.isInRect(x, y, recLT)) {
            recFlag = CropCanvas.PRESS_LT;
            returnFlag = true;
        } else if (this.isInRect(x, y, recRB)) {
            recFlag = CropCanvas.PRESS_RB;
            returnFlag = true;
        } else if (this.isInRect(x, y, recRT)) {
            recFlag = CropCanvas.PRESS_RT;
            returnFlag = true;
        } else if (this.isInRect(x, y, recL)) {
            recFlag = CropCanvas.PRESS_L;
            returnFlag = true;
        } else if (this.isInRect(x, y, recT)) {
            recFlag = CropCanvas.PRESS_T;
            returnFlag = true;
        } else if (this.isInRect(x, y, recR)) {
            recFlag = CropCanvas.PRESS_R;
            returnFlag = true;
        } else if (this.isInRect(x, y, recB)) {
            recFlag = CropCanvas.PRESS_B;
            returnFlag = true;
        }

        return returnFlag;
    }

    public boolean isInRect(int x, int y, RectF rect) {
        if (x >= rect.left - 20 && x <= rect.right + 20 && y > rect.top - 20 && y < rect.bottom + 20) {
            return true;
        }
        return false;
    }

    //点击角落矩形框改变选择区域大小时，不能超过图片所在的区域
    //下面以press打头的四个函数就是判断是否超出图片区域
    //如果超出了，就移动不了
    //不超出按照触笔移动的距离来移动小矩形框

    //pressLB是当点击左下角小矩形框改变大小时是否超出图片区域
    //如果超出就将left与bottom的值设为图片区域的left和bottom
    private void pressLB(int x, int y) {
        float left = ChooseArea.left + x;
        float right = ChooseArea.right;
        float top = ChooseArea.top;
        float bottom = ChooseArea.bottom + y;
        if (left <= right - MIN_SIZE && left >= dst.left && bottom <= dst.bottom && bottom >= top + MIN_SIZE) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (left + x < dst.left) {
                left = dst.left;
            }

            if (bottom + y > dst.bottom) {
                bottom = dst.bottom;
            }

            if (ChooseArea.left + x > ChooseArea.right - MIN_SIZE) {
                left = ChooseArea.right - MIN_SIZE;
            }

            if (ChooseArea.bottom + y < ChooseArea.top + MIN_SIZE) {
                bottom = ChooseArea.top + MIN_SIZE;
            }
            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    //pressLT是当点击左上角小矩形框改变大小时是否超出图片区域
    //如果超出就将left与top的值设为图片区域的left和top
    private void pressLT(int x, int y) {
        float left = ChooseArea.left + x;
        float right = ChooseArea.right;
        float top = ChooseArea.top + y;
        float bottom = ChooseArea.bottom;
        if (left <= right - MIN_SIZE && left >= dst.left && top <= bottom - MIN_SIZE && top >= dst.top) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (left < dst.left) {
                left = dst.left;
            }

            if (top < dst.top) {
                top = dst.top;
            }

            if (left > right - MIN_SIZE) {
                left = right - MIN_SIZE;
            }

            if (top > bottom - MIN_SIZE) {
                top = bottom - MIN_SIZE;
            }
            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    //pressRT是当点击右上角小矩形框改变大小时是否超出图片区域
    //如果超出就将right与top的值设为图片区域的right和top
    private void pressRT(int x, int y) {
        float left = ChooseArea.left;
        float right = ChooseArea.right + x;
        float top = ChooseArea.top + y;
        float bottom = ChooseArea.bottom;

        if (right <= dst.right && right >= left + MIN_SIZE && top <= bottom - MIN_SIZE && top >= dst.top) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (right > dst.right) {
                right = dst.right;
            }

            if (top < dst.top) {
                top = dst.top;
            }

            if (right < left + MIN_SIZE) {
                right = left + MIN_SIZE;
            }

            if (top > bottom - MIN_SIZE) {
                top = bottom - MIN_SIZE;
            }
            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    //pressRB是当点击右下角小矩形框改变大小时是否超出图片区域
    //如果超出就将right与bottom的值设为图片区域的right和bottom
    private void pressRB(int x, int y) {
        float left = ChooseArea.left;
        float right = ChooseArea.right + x;
        float top = ChooseArea.top;
        float bottom = ChooseArea.bottom + y;

        if (right <= dst.right && right >= left + MIN_SIZE && bottom <= dst.bottom && bottom >= top + MIN_SIZE) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (right > dst.right) {
                right = dst.right;
            }

            if (bottom > dst.bottom) {
                bottom = dst.bottom;
            }

            if (right < left + MIN_SIZE) {
                right = left + MIN_SIZE;
            }

            if (bottom < top + MIN_SIZE) {
                bottom = top + MIN_SIZE;
            }
            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    private void pressL(int x) {
        float left = ChooseArea.left + x;
        float right = ChooseArea.right;
        float bottom = ChooseArea.bottom;
        float top = ChooseArea.top;

        if (left <= right - MIN_SIZE && left >= dst.left) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (left < dst.left) {
                left = dst.left;
            }

            if (left > right - MIN_SIZE) {
                left = right - MIN_SIZE;
            }

            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    private void pressT(int y) {
        float left = ChooseArea.left;
        float right = ChooseArea.right;
        float top = ChooseArea.top + y;
        float bottom = ChooseArea.bottom;
        if (top <= bottom - MIN_SIZE && top >= dst.top) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (top < dst.top) {
                top = dst.top;
            }

            if (top > bottom - MIN_SIZE) {
                top = bottom - MIN_SIZE;
            }

            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    private void pressR(int x) {
        float left = ChooseArea.left;
        float right = ChooseArea.right + x;
        float top = ChooseArea.top;
        float bottom = ChooseArea.bottom;

        if (right <= dst.right && right >= left + MIN_SIZE) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (right > dst.right) {
                right = dst.right;
            }

            if (right < left + MIN_SIZE) {
                right = left + MIN_SIZE;
            }

            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    private void pressB(int y) {
        float left = ChooseArea.left;
        float right = ChooseArea.right;
        float top = ChooseArea.top;
        float bottom = ChooseArea.bottom + y;

        if (bottom <= dst.bottom && bottom >= top + MIN_SIZE) {
            ChooseArea.set(left, top, right, bottom);
        } else {
            if (bottom > dst.bottom) {
                bottom = dst.bottom;
            }

            if (bottom < top + MIN_SIZE) {
                bottom = top + MIN_SIZE;
            }

            ChooseArea.set(left, top, right, bottom);
        }
        setPressRecLoc();
    }

    //每次改变选择区域矩形的大小或者移动，各角落上的小矩形也要改变它的Location
    private void setPressRecLoc() {
        recLT.set(ChooseArea.left - CORNER_SIZE, ChooseArea.top - CORNER_SIZE, ChooseArea.left + CORNER_SIZE,
                ChooseArea.top + CORNER_SIZE);
        recLB.set(ChooseArea.left - CORNER_SIZE, ChooseArea.bottom - CORNER_SIZE, ChooseArea.left + CORNER_SIZE,
                ChooseArea.bottom + CORNER_SIZE);
        recRT.set(ChooseArea.right - CORNER_SIZE, ChooseArea.top - CORNER_SIZE, ChooseArea.right + CORNER_SIZE,
                ChooseArea.top + CORNER_SIZE);
        recRB.set(ChooseArea.right - CORNER_SIZE, ChooseArea.bottom - CORNER_SIZE, ChooseArea.right + CORNER_SIZE,
                ChooseArea.bottom + CORNER_SIZE);

        recL.set(ChooseArea.left - MARGIN_WIDTH / 2, (ChooseArea.top + ChooseArea.bottom - MARGIN_HEIGHT) / 2,
                ChooseArea.left + MARGIN_WIDTH / 2, (ChooseArea.top + ChooseArea.bottom + MARGIN_HEIGHT) / 2);
        recT.set((ChooseArea.left + ChooseArea.right - MARGIN_HEIGHT) / 2, ChooseArea.top - MARGIN_WIDTH / 2,
                (ChooseArea.left + ChooseArea.right + MARGIN_HEIGHT) / 2, ChooseArea.top + MARGIN_WIDTH / 2);
        recR.set(ChooseArea.right - MARGIN_WIDTH / 2, (ChooseArea.top + ChooseArea.bottom - MARGIN_HEIGHT) / 2,
                ChooseArea.right + MARGIN_WIDTH / 2, (ChooseArea.top + ChooseArea.bottom + MARGIN_HEIGHT) / 2);
        recB.set((ChooseArea.left + ChooseArea.right - MARGIN_HEIGHT) / 2, ChooseArea.bottom - MARGIN_WIDTH / 2,
                (ChooseArea.left + ChooseArea.right + MARGIN_HEIGHT) / 2, ChooseArea.bottom + MARGIN_WIDTH / 2);
    }

    //判断触笔是否在选择区域内
    public boolean judgeLocation(float x, float y) {
        float start_x = this.getChooseArea().left;
        float start_y = this.getChooseArea().top;
        float last_x = this.getChooseArea().right;
        float last_y = this.getChooseArea().bottom;
        //System.out.println("chubi:" + x + "," + y);
        //System.out.println(start_y + "," + last_y);
        if (x > start_x + 10 && x < last_x - 10 && y > start_y + 10 && y < last_y - 10) {
            return true;
        }
        return false;
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (firstFlag) {
            dst.set(0, 0, getWidth(), getHeight());
            firstFlag = false;
            mPaint.setColor(COLOR_NORMAL);

            if (ChooseArea == null) {
                ChooseArea = new RectF((dst.width() - mDefaultChooseAreaWidth) / 2,
                        (dst.height() - mDefaultChooseAreaHeight) / 2, (dst.width() + mDefaultChooseAreaWidth) / 2,
                        (dst.height() + mDefaultChooseAreaHeight) / 2);
                setPressRecLoc();
            }

            if (SmallphoneScannerActivity.LOG) {
                Log.d(TAG, "dest Width: " + (dst.right - dst.left));
                Log.d(TAG, "dest Height: " + (dst.bottom - dst.top));
                Log.d(TAG, "View Width: " + getWidth());
                Log.d(TAG, "View Height: " + getHeight());
                Log.d(TAG, ChooseArea.left + "|" + ChooseArea.top + "|" + ChooseArea.right + "|" + ChooseArea.bottom);
            }
        }

        set_LeftArea_Alpha();
        canvas.drawRect(leftRectL, leftAreaPaint);
        canvas.drawRect(leftRectR, leftAreaPaint);
        canvas.drawRect(leftRectT, leftAreaPaint);
        canvas.drawRect(leftRectB, leftAreaPaint);

        canvas.drawRect(ChooseArea, mPaint);

        final BitmapDrawable cornerDrawable = (BitmapDrawable) getResources()
                .getDrawable(R.drawable.crop_canvas_corner);
        final Bitmap cornerBitmap = cornerDrawable.getBitmap();

        canvas.drawBitmap(cornerBitmap, null, recLT, mPaint);
        canvas.drawBitmap(cornerBitmap, null, recLB, mPaint);
        canvas.drawBitmap(cornerBitmap, null, recRT, mPaint);
        canvas.drawBitmap(cornerBitmap, null, recRB, mPaint);

        final BitmapDrawable marginhDrawable = (BitmapDrawable) getResources().getDrawable(
                R.drawable.crop_canvas_margin_h);
        final Bitmap marginhBitmap = marginhDrawable.getBitmap();
        canvas.drawBitmap(marginhBitmap, null, recT, mPaint);
        canvas.drawBitmap(marginhBitmap, null, recB, mPaint);

        final BitmapDrawable marginvDrawable = (BitmapDrawable) getResources().getDrawable(
                R.drawable.crop_canvas_margin_v);
        final Bitmap marginvBitmap = marginvDrawable.getBitmap();
        canvas.drawBitmap(marginvBitmap, null, recL, mPaint);
        canvas.drawBitmap(marginvBitmap, null, recR, mPaint);
    }

    public void set_LeftArea_Alpha() {
        leftRectL.set(dst.left, dst.top, ChooseArea.left, dst.bottom);
        leftRectR.set(ChooseArea.right, dst.top, dst.right, dst.bottom);
        leftRectT.set(ChooseArea.left, dst.top, ChooseArea.right, ChooseArea.top);
        leftRectB.set(ChooseArea.left, ChooseArea.bottom, ChooseArea.right, dst.bottom);
    }
}
